//
//  ButtonView.m
//  ButtonView
//
//  Created by hcios on 2017/6/24.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "ButtonView.h"

@implementation ButtonView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        [self setUpButton];
    }
    return self;
}


-(void)setUpButton{
    CGFloat half_h = self.frame.size.height / 2;
    CGFloat half_w = self.frame.size.width / 2;
    
    //定义按钮名称数组
    NSArray *buttonNameArr = @[@"语音翻译",@"汇率转换",@"消费记录",@"WiFi查询"];
    //按钮图片数组
     NSArray *buttonImageArr = @[@"翻译",@"汇率转换",@"消费记录",@"wifi"];
    
    
    //创建按钮
    for (int i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        if (i % 2 == 1) {
            [button setFrame:CGRectMake(half_w, (i / 2) * half_h, half_w, half_h)];
        }else{
            [button setFrame:CGRectMake(0, (i / 2) * half_h, half_w, half_h)];
        }
        
        button.tag = 50 + i;

        //赋值
        [button setTitle:buttonNameArr[i] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:buttonImageArr[i]] forState:UIControlStateNormal];
        
        //布局约束
        [button setTitleEdgeInsets:UIEdgeInsetsMake(half_h - 30, -64, 0, 0)];
        [button setImageEdgeInsets:UIEdgeInsetsMake((half_h - 94) / 2, (half_w - 64) / 2, (half_h - 94) / 2, (half_w - 64) / 2)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ];
        
        
        //基本设置
        [button.titleLabel setFont:[UIFont systemFontOfSize:16]];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor clearColor];
        
        //点击事件
        [button addTarget:self action:@selector(didClick:) forControlEvents:UIControlEventTouchUpInside];
        
        //添加button
        [self addSubview:button];

    }
}

-(void)didClick:(id)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClick:)]) {
        [self.delegate didClick:sender];
    }
}

@end
